import React, {Component} from 'react'
import {Button, Col, Tooltip} from 'antd'
import '../../less/index.less'
import {connect} from 'react-redux'
import {Link, withRouter} from 'react-router-dom'
import {FormattedMessage} from 'react-intl'
import {convertContent} from '../../constant/lang-util'

class ReferenceButtonGroup extends Component {

  goExcellentAssignmentList() {
    const {studentWebUrl} = this.props.settings
    const url = `${studentWebUrl}/program/${this.props.programId}/task/${this.props.taskId}/assignment/${this.props.assignmentId}/quiz/${this.props.quizId}/excellent-quiz-list`
    window.location.assign(url)
  }

  showExcellentOnlineCodingVideo() {
    const {programId, taskId, assignmentId, studentId, history} = this.props
    const commonUrl = `program/${programId}/task/${taskId}/assignment/${assignmentId}/excellent-assignment-video`
    const tutorUrl = `/tutor/program/${programId}/task/${taskId}/student/${studentId}/assignment/${assignmentId}/excellent-assignment-video`

    this.props.isTutor
      ? history.push(tutorUrl)
      : this.props.isInstructor
      ? history.push(`/preview/${commonUrl}`)
      : history.push(`/student/${commonUrl}`)
  }

  render() {
    const {programId, taskId, assignmentId, studentId} = this.props
    const review = this.props.review || {}
    const tutor = this.props.isTutor
    const instructor = this.props.isInstructor
    const disable = tutor ? '' : instructor ? '' : review.status && review.status !== '已提交' ? '' : true
    const title = tutor ? '' : instructor ? '' : review.status ? '' : convertContent('完成作业才能看呦')
    const quizDefinition = this.props.quizDefinition || {}
    const answerPath = quizDefinition.answerPath

    const isShowExcellentOnlineCoding = tutor || instructor ? 'hidden' : ''

    const tutorAnswer = `/tutor/program/${programId}/task/${taskId}/student/${studentId}/assignment/${assignmentId}/quizzes/${quizDefinition.id}/online-language-answer`
    const commonUrl = `program/${programId}/task/${taskId}/assignment/${assignmentId}/quizzes/${quizDefinition.id}/online-language-answer`

    const studentAnswer = `/student/${commonUrl}`
    const instructorAnswer = `/preview/${commonUrl}`

    const url = tutor ? tutorAnswer : instructor ? instructorAnswer : studentAnswer

    return (<div>
        <Col>
          <div>
            {answerPath
              ? <Tooltip title={title}>
                <Link to={url}><Button disabled={disable}><FormattedMessage id='参考答案'/></Button></Link>
              </Tooltip> : ''
            }
            <span className={isShowExcellentOnlineCoding}>
            <Tooltip title={title}>
              <Button disabled={disable}
                      className={'margin-l-2'}
                      onClick={this.goExcellentAssignmentList.bind(this)}>
                <FormattedMessage id='优秀作业'/>
              </Button>
            </Tooltip>
          </span>
          </div>
        </Col>
      </div>
    )
  }
}

const mapStateToProps = ({settings}) => ({
  settings
})

const mapDispatchToProps = dispatch => ({})

export default withRouter(connect(mapStateToProps, mapDispatchToProps)(ReferenceButtonGroup))
